package com.runvp.handler;

import com.runvp.common.constant.ConstantRedisKey;
import com.runvp.common.core.redis.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

@Slf4j
@Component
public class SendCmdHandler {

    private RedisCache redisCache;



    public static SendCmdHandler instance() {
        return new SendCmdHandler();
    }

    public SendCmdHandler() {
    }

    public SendCmdHandler(RedisCache redisCache) {
        this.redisCache = redisCache;
    }

    /**
     * 发送充电指令
     *
     * @param orderNo
     * @param outOrderNo
     * @param sn
     * @param body
     */
    public boolean sendOutCmd(String orderNo, String outOrderNo, String sn, String body) {
        //发送充电指令
        int count = 0;
        String msg = "";
        while (true) {
            String sendFlag = redisCache.getCacheObject(ConstantRedisKey.ORDER + outOrderNo);
            if (StringUtils.isNotBlank(sendFlag)) {
                msg = "订单【" + orderNo + "】==>充电订单号【" + outOrderNo + "】充电指令发成功";
                log.info("sendOutCmd:::{}", msg);
                return true;
            }
            if (count > 6) {
                String errorMsg = "订单【" + orderNo + "】==>充电订单号【" + outOrderNo + "】充电指令发失败或超时";
                log.error("sendOutCmd:::errorMsg:{}", errorMsg);
                return false;
            }
            count++;
            try {
                Thread.sleep(500);
            } catch (Exception e) {
            }
        }
    }
}
